package runesim.node;

import runesim.node.model.Player;
import runesim.node.model.World;
import runesim.node.packetbuilder.PlayerUpdate;
import runesim.node.util.EntityList;

public final class ClientUpdater {
	
	private EntityList<Player> players = World.getWorld().getPlayers();
	private PlayerUpdate playerUpdate = new PlayerUpdate();
	
	public ClientUpdater() {
		World.getWorld().setClientUpdater(this);
	}
	
	/**
	 * Update player/NPC movements
	 */
	public void majorUpdate() {
		// Change player positions and remove logged out players
		for(Player p : players) {
			if(!p.isLoggedIn()) {
				World.getWorld().unregisterPlayer(p, true);
				continue;
			}
			if(!p.isMapAreaChanged()) {
				p.getPathHandler().updatePosition();
				if(p.isRunning())
					p.getPathHandler().updatePosition();
			}
			p.updateAppearanceId();
		}
		
		for(Player p : players) {
			p.validatedWatchedPlayers();
			p.updateWatchedPlayers();
		}
		
		// This loop updates player info
		for(Player p : players) {
			if(p.isMapAreaChanged()) {
				p.getActionSender().sendMapArea();
				p.setKnownRegion(p.getLocation().getRegionX(), p.getLocation().getRegionY());
			}
			p.getIoSession().write(playerUpdate.getPacket(p));
		}
		
		// This loop resets player data
		for(Player p : players) {
			p.getAppearance().setChanged(false);
			p.setMapAreaChanged(false);
			p.setSprite(-1); // reset player movement.
			p.setLastChatMessage(null);
			p.setTextOverheadMessage(null);
			p.resetEmote();
			p.getWatchedPlayers().update();
		}
	}
	
	/**
	 * Update player/NPC appearances, game objects, items, wall objects, ping
	 */
	public void minorUpdate() {

	}
}
